package com.spark.lola.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spark.lola.entity.warehouse.StoreInfo;
import com.spark.lola.service.IProductInfoService;
import com.spark.lola.service.IStoreInfoService;
import com.spark.lola.service.IStoreProductService;
import com.spark.lola.support.common.util.TimeUtil;
import com.spark.lola.support.warehouse.configuration.WarehouseKafkaTopic;
import com.spark.lola.support.warehouse.service.FeiZhiTongServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 废纸通 同步任务
 * @Author: DengYeJun
 * @Date: 2020/6/10 14:39
 */
@Component
public class FeiZhiTongJob {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IStoreInfoService iStoreInfoService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 同步货品
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void syncProduct() {
        kafkaTemplate.send(WarehouseKafkaTopic.SYNC_PRODUCT,"syn-product");
    }

    /**
     * 每五分钟拉取一次订单
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void syncOrder() throws InterruptedException {
        List<StoreInfo> stores = iStoreInfoService.getStores();

        JSONObject request = new JSONObject();
        request.put("start", TimeUtil.getCurrentDate());
        request.put("end", TimeUtil.getCurrentDate());
        for(StoreInfo o:stores){
            kafkaTemplate.send(WarehouseKafkaTopic.SYNC_STORE_ORDER,o.getNo(), request.toJSONString());
            Thread.sleep(5*1000);
        }

    }
}
